package com.yxhl.km;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.yxhl.km.base.BaseActivity;
import com.yxhl.km.module.home.HomeFragment;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.message.MessageFragment;
import com.yxhl.km.module.person.PersonFragment;
import com.yxhl.km.module.track.TrackFragment;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.SharedPreferencesUtils;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.module.widget.MainNavigateTabBar;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
 * 首页Activity
 */
public class HomeActivity extends BaseActivity {
    private MainNavigateTabBar mNavigateTabBar;
    private Badge badge;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initListener() {

    }


    @Override
    protected void initialized(Bundle savedInstanceState) {
        mNavigateTabBar = (MainNavigateTabBar) findViewById(R.id.mainTabBar);
        mNavigateTabBar.onRestoreInstanceState(savedInstanceState);
        initTab();
    }

    /**
     *   初始化首页四大页面
     */
    private void initTab() {
        mNavigateTabBar.addTab(HomeFragment.class, new MainNavigateTabBar.TabParam(R.mipmap.tab_first_grey, R.mipmap.tab_first_nor, Constants.TAG_PAGE_HOME));
        mNavigateTabBar.addTab(TrackFragment.class, new MainNavigateTabBar.TabParam(R.mipmap.tab_four_grey, R.mipmap.tab_four_nor, Constants.TAG_PAGE_TRACK));
        mNavigateTabBar.addTab(null, new MainNavigateTabBar.TabParam(0, 0, ""));
        mNavigateTabBar.addTab(MessageFragment.class, new MainNavigateTabBar.TabParam(R.mipmap.tab_second_grey, R.mipmap.tab_second_nor, Constants.TAG_PAGE_MESSAGE));
        mNavigateTabBar.addTab(PersonFragment.class, new MainNavigateTabBar.TabParam(R.mipmap.tab_third_grey, R.mipmap.tab_third_nor, Constants.TAG_PAGE_PERSION));
        badge = new QBadgeView(this).bindTarget(mNavigateTabBar.getChildAt(3).findViewById(R.id.tab_icon)).setBadgeNumber(5);
        badge.setBadgeGravity(Gravity.END | Gravity.TOP);
        badge.setBadgeTextSize(8, true);
        badge.setBadgePadding(1, true);
        badge.setGravityOffset(0, 0, true);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mNavigateTabBar.onSaveInstanceState(outState);
    }

    /**
     * 连续两次返回键退出
     */
    private long firstPressed;
    private long dowableClick = 1000 * 2;//连续返回两次退出应用

    @Override
    public void onBackPressed() {
        long now = System.currentTimeMillis();
        if ((now - firstPressed <= dowableClick) && firstPressed != 0) {
            super.onBackPressed();
            //销毁当前页面
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } else {
            ToastUtil.makeToast("再按一次退出应用", false);
            firstPressed = now;
        }
    }

    public void onClickPublish(View v) {
        Toast.makeText(this, "发布", Toast.LENGTH_LONG).show();
    }
}
